package com.qf.smsplatform.strategy.strategy.Impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.strategy.client.CacheClient;
import com.qf.smsplatform.strategy.strategy.Strategy;
import com.qf.smsplatform.strategy.util.PushErrorMessageUtil;
import com.qf.smsplatform.strategy.wordfilter.WordContext;
import com.qf.smsplatform.strategy.wordfilter.WordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * projectName: smsplatform
 *
 * @author: MaTianWei
 * time: 2021/8/12 20:49
 * description:短信内容敏感词校验
 */
@Service(value = "dirtyWord")
@Slf4j
public class DirtyWordStrategyImpl implements Strategy {
    @Autowired
    private CacheClient cacheClient;
    //TODO 敏感词校验    已完成
    @Override
    public boolean execute(StandardSubmit submit) {
        log.info("[策略模块-敏感词校验] =====开始执行=====");
        //1获取用户的短信内容,并且拼接DIRTYWORDS:(key) 然后去redis中比较
        String messageContent = submit.getMessageContent();
        //短信切分词
        WordFilter filter = new WordFilter(new WordContext());
        boolean filterFlag = filter.include(messageContent);
        //判断是否有敏感词
        if(filterFlag){
            PushErrorMessageUtil.sendLog(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            PushErrorMessageUtil.sendReport(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            log.info("【策略模块-敏感词校验】 所发信息包含敏感词! messageContent = {}", messageContent);
            return false;
        }
        log.info("[策略模块-敏感词校验] =====执行成功=====");
        return true;
    }
}
